angular.module('forgetcode', [])
    .controller('forgetcodeCtrl', ['$scope', 'httpService', '$interval', '$state', 'alertmsg', function($scope, httpService, $interval, $state, alertmsg) {

        $scope.reset = {
            type: 'tel'
        };

        // 发送手机验证码
        var regx = /(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
        $scope.count_down_time = interval;
        $scope.isDisplayTime = false;
        $scope.sendTel = function(tel) {
            if (regx.test(tel)) {
                httpService.query('web/send_code', { tel: tel }, 'post').then(function(res) {
                    if (res.code == 200) {
                        countDown();
                    } else {
                        alertmsg(res.msg);
                    }
                });
            } else {
                alertmsg('请正确输入手机号码！');
            }
        };

        // 发送邮箱验证码
        $scope.sendEmail = function(email) {
            var regx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (regx.test(email)) {
                httpService.query('web/send_mail', { email: email }, 'post').then(function(res) {
                    if (res.code == 200) {
                        alertmsg('邮箱发送成功，请注意查收!');
                        // $state.go('forget', { data: JSON.stringify(email)});
                    } else {
                        alertmsg(res.msg);
                    }
                });
            } else {
                alertmsg('请正确输入邮箱！');
            }
        };

        $scope.valideCode = function(msg) {
            msg.code = msg.code + '';
            httpService.query('web/valid_code', msg, 'post').then(function(res) {
                if (res.code == 200) {
                    $state.go('forget', { data: JSON.stringify(msg)});
                } else {
                    alertmsg(res.msg);
                }
            });
        }

        // 倒计时方法
        function countDown() {
            var timer;
            // 显示倒计时时间
            $scope.isDisplayTime = true;
            timer = $interval(function() {
                $scope.count_down_time -= 1;
                // 当倒计时结束时，结束显示正常
                if ($scope.count_down_time <= 0) {
                    // 结束倒计时器
                    $interval.cancel(timer);
                    // 将倒计时重新附值
                    $scope.count_down_time = interval;
                    // 设置为可提交状态
                    $scope.isabled = false;
                    // 显示为获取验证码
                    $scope.isDisplayTime = false;
                }
            }, 1000);
        };

    }]);
